import axios from 'axios';
// import {getToken,removeToken} from "./auth"
// import { Message } from 'element-ui';
// console.log(getStore("token"))
// import store from "@/store/index"
// import router from '../router'
import CONSTANT from '@/config/CONSTANT'


axios.defaults.baseURL = CONSTANT.baseUrl
axios.defaults.timeout = 10000;
axios.defaults.withCredentials = false
// axios.defaults.headers.common['token'] = getToken()==null?"":getToken();
axios.defaults.headers = {
  "Content-Type": "application/json",
};

// 添加请求拦截器
axios.interceptors.request.use(
  config => {
       // 让每个请求携带自定义token 请根据实际情况自行修改
    //   if (store.getters.getToken) {
    //     config.headers['token'] = getToken() 
       
    //   }
      return config
  },
  error => {
      // Do something with request error
      console.log(error) // for debug
      Promise.reject(error)
  }
)

// 添加响应拦截器
axios.interceptors.response.use(
  response => {
      if(response.status==200){
        // if(response.data.status == "0"){
        //     Message({message:`${response.data.message}`,type:"error"})
        // }
        // if(response.data.status == "1"&&response.data.message!=""){
        //     Message({message:`${response.data.message}`,duration:"1000",type:"success"})
        //     return response
        // }
        // if(response.data.status == "1"&&response.data.message==""){
        //     return response
        // }
        // if(response.data.status == "403"){
        //     Message({message:`${response.data.message}`,duration:"1000",type:"error"})
        //     removeToken()
        //     router.push({path:"/login"})
        // }
     
            

      }
      
  },
  error => {
      console.log('err' + error) // for debug
      return Promise.reject(error)
  }
)

/**
 * get方法，对应get请求
 * @param {String} url [请求的url地址]
 * @param {Object} params [请求时携带的参数]
 */
export function get(url, params){    
    return new Promise((resolve, reject) =>{        
        axios.get(url, {            
            params: params        
        }).then(res => {
            resolve(res.data);
        }).catch(err =>{
            reject(err.data)        
    })    
});}
/** 
 * post方法，对应post请求 
 * @param {String} url [请求的url地址] 
 * @param {Object} params [请求时携带的参数] 
 */
export function post(url, params) {
    return new Promise((resolve, reject) => {
         axios.post(url, params)
        .then(res => {
            resolve(res.data);
        })
        .catch(err =>{
            reject(err.data)
        })
    });
}


